package com.core.gui.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import com.core.dao.impl.DBUtil;
import com.core.dao.impl.UsernameDAOImpl;
import com.core.entities.Username;
import com.core.gui.helper.UserInfo;
import com.core.service.UsernamSrv;

import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChangePasswordDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final JLabel lblUsername = new JLabel("Username:");
	public static JLabel usernameLabel = new JLabel("");
	private final JLabel lblOldPassword = new JLabel("Old Password :");
	private final JPasswordField oldPasswordField = new JPasswordField();
	private final JLabel lblNewPassword = new JLabel("New Password :");
	private final JPasswordField newPasswordField = new JPasswordField();
	private final JLabel lblReenterNewPassword = new JLabel(
			"<html>\r\n<body>\r\n<pre>Re-Enter\r\nNew Password :\r\n</pre>\r\n</body>\r\n</html>\r\n");
	private final JPasswordField reNewPasswordField = new JPasswordField();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			ChangePasswordDialog dialog = new ChangePasswordDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public ChangePasswordDialog() {
		setBounds(100, 100, 326, 201);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		lblUsername.setBounds(10, 11, 52, 14);

		contentPanel.add(lblUsername);
		usernameLabel.setHorizontalAlignment(SwingConstants.CENTER);
		usernameLabel.setBounds(72, 11, 108, 14);

		contentPanel.add(usernameLabel);
		lblOldPassword.setBounds(10, 36, 72, 14);

		contentPanel.add(lblOldPassword);
		oldPasswordField.setBounds(82, 33, 156, 20);

		contentPanel.add(oldPasswordField);
		lblNewPassword.setBounds(10, 61, 77, 14);

		contentPanel.add(lblNewPassword);
		newPasswordField.setBounds(92, 58, 146, 20);

		contentPanel.add(newPasswordField);
		lblReenterNewPassword.setBounds(10, 86, 98, 42);

		contentPanel.add(lblReenterNewPassword);
		reNewPasswordField.setBounds(113, 95, 136, 20);

		contentPanel.add(reNewPasswordField);
		usernameLabel.setText(UserInfo.getInstance().getUser().getUsername());

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
validatFields();
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	private void getData() {
		try {

			UserInfo.getInstance().getService().getUsernamSrv().changePassword(
					UserInfo.getInstance().getUser(), oldPasswordField.getText(),
							newPasswordField.getText(),
							reNewPasswordField.getText());
			dispose();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void validatFields() {
//		LoginDialog loginDialog = new LoginDialog();
		
		
		String miss = "Missing data of :" + "\n";
		if (oldPasswordField.getText().isEmpty()
				|| newPasswordField.getText().isEmpty()
				|| reNewPasswordField.getText().isEmpty()) {

			if (oldPasswordField.getText().isEmpty()) {
				miss += "Username.\n";
			}
			if (newPasswordField.getText().isEmpty()) {
				miss += "Password.\n";
			}
			if (reNewPasswordField.getText().isEmpty()) {
				miss += "Repeat Password.\n";
			}
			JOptionPane.showMessageDialog(null, miss, null,
					JOptionPane.ERROR_MESSAGE);

		}
		if (!newPasswordField.getText().equals(reNewPasswordField.getText())) {
			JOptionPane.showMessageDialog(null,
					"Repeat password not match with new password", null,
					JOptionPane.ERROR_MESSAGE);
		} else {

			getData();

		}
	}
	
//	public void getUsernameData(String text){
//		usernameLabel.setText(text);
//	}

}
